//
//  RTJLendController.m
//  RuiTaiJinProject
//
//  Created by jayson on 2018/11/26.
//  Copyright © 2018年 leewei. All rights reserved.
//

#import "RTJLendController.h"
#import "RTJHomeCell.h"
#import "RTJTenderDetailsController.h"
#import "RTJAutomaticBidController.h"
#import "RTJAutomaticBidDetailsController.h"
#import "RTJAutoMarkDetailsController.h"
@interface RTJLendController ()

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *data;
@property (nonatomic, strong) NSDictionary *dict;
@property(nonatomic, assign) NSInteger currentPage;
@property (nonatomic,assign) BOOL isLoaded;

@end

@implementation RTJLendController

-(NSMutableArray *)data {
    if (!_data) {
        _data = [NSMutableArray array];
    }
    
    return _data;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"出借";
    self.tableView.tableFooterView=[UIView new];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.estimatedRowHeight=300;
   
    self.currentPage = 1;
    self.tableView.mj_header = [CustomRefreshGifHeader headerWithRefreshingBlock:^{
        
        self.currentPage = 1;
        [self loadData];
        
    }];
    
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        
        self.currentPage ++;
        [self loadData];
        
    }];
    
    [self.tableView.mj_header beginRefreshing];

    
}

-(void)loadData{
    
    [SVProgressHUD  showWithStatus:nil];
    
    if (self.data.count == 0) {
        
        [self.view showEmptyViewWithStr:@"" image:nil callback:nil];
        
    }
    
    
    [RTJNetWork asyncRequestWithURL:@"deals/index" params:@{@"page":@(_currentPage)} method:GET zxCompletion:^(id data, NSInteger status, BOOL success, NSString *errorMsg) {
        
        [SVProgressHUD dismiss];
        [self.view removeEmptyView];
        [self.tableView.mj_footer endRefreshing];
        [self.tableView.mj_header endRefreshing];
        
        if (success) {
            
            if ([data[@"status"]integerValue] == 1) {
                
                self.dict = data[@"data"];

                if ( [data[@"data"][@"deal_list"][@"list"] isKindOfClass:[NSArray class]]) {
                   
                    NSArray *arr = [NSArray array];
                    
                    arr = data[@"data"][@"deal_list"][@"list"];
                    
                    if (arr.count > 0) {
                        
                        if (self.currentPage == 1) {
                            
                            [self.data removeAllObjects];
                        }
                        
                        [self.data addObjectsFromArray:arr];
                        
                        if (arr.count < ZXPAGE_SIZE) {
                            
                            [self.tableView.mj_footer endRefreshingWithNoMoreData];
                        }
                        
                    }else{
                        
                        if (self.currentPage == 1) {
                            
                            [self.data removeAllObjects];
                            
//                            [self.view showEmptyViewWithStr:@"暂无相关数据" image:[UIImage imageNamed:@"kbylogo"] callback:^{
//                                [self loadData];
//                            }];
                            
                            [self.tableView.mj_footer endRefreshingWithNoMoreData];

                            
                        }else{
                            
                            [self.tableView.mj_footer endRefreshingWithNoMoreData];
                            
                        }
                    }
                    
                    _isLoaded = YES;
                    [self.tableView reloadData];

                }else{
                    
                    [self.tableView.mj_footer endRefreshingWithNoMoreData];
                    [self.tableView reloadData];

                }
                
            }else{

                [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"%@",data[@"msg"]]];

                [self.view showEmptyViewWithStr:errorMsg image:[UIImage imageNamed:@"kbylogo"] callback:^{
                    [self loadData];
                }];

            }
            
        }else{
            
            [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"%@",errorMsg]];
            
            [self.view showEmptyViewWithStr:errorMsg image:[UIImage imageNamed:@"kbylogo"] callback:^{
                [self loadData];
            }];

        }
     
    }];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 3;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (section == 0) {
        
        if ([self.dict[@"new_deal"] isEqual:[NSDictionary class]]) {
            
            return 2;
            
        }else{
            
            return _isLoaded ? 1 : 0;
        }
        
    }else if (section == 1){
        
        if ( [self.dict[@"plan_list"] count]!=0) {
            
            return 1 + [self.dict[@"plan_list"] count];
            
        }else{
            
            return 0;
        }
        
    }else{
        
        if (self.data.count!=0) {
            
            return self.data.count+1;
            
        }else{
            
            return 0;
        }
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return UITableViewAutomaticDimension;
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * ID = @"RTJHomeCell";
    
    RTJHomeCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    if (indexPath.section == 0) {
        
        if (indexPath.row == 0) {
            
            if (!cell) {
                
                cell = [[NSBundle mainBundle] loadNibNamed:@"RTJHomeCell" owner:self options:nil][8];
            }
            cell.lenddata = self.dict;

        }else{
            
            if (!cell) {
                
                cell = [[NSBundle mainBundle] loadNibNamed:@"RTJHomeCell" owner:self options:nil][1];
            }
            cell.lenddata = self.dict;
        }
        
    }else if (indexPath.section == 1){
        
        
            if (indexPath.row == 0) {
                
                if (!cell) {
                    
                    cell = [[NSBundle mainBundle] loadNibNamed:@"RTJHomeCell" owner:self options:nil][5];
                }
                
                cell.headerTitle.text = @"自动投标服务";
                
            }else {
                
                if (!cell) {
                    
                    cell = [[NSBundle mainBundle] loadNibNamed:@"RTJHomeCell" owner:self options:nil][4];
                }
                
                if ( [self.dict[@"plan_list"] count]!=0) {
                    
                    cell.autodata = self.dict[@"plan_list"][indexPath.row-1];

                }
            }
        
    }else {
        
            if (indexPath.row == 0) {
                
                if (!cell) {
                    
                    cell = [[NSBundle mainBundle] loadNibNamed:@"RTJHomeCell" owner:self options:nil][5];
                }
                cell.headerTitle.text = @"优质项目";
                
            }else{
                
                if (!cell) {
                    
                    cell = [[NSBundle mainBundle] loadNibNamed:@"RTJHomeCell" owner:self options:nil][3];
                }
                cell.markData = self.data [indexPath.row-1];
            }
    }
    
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    return  cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        
        if (indexPath.row == 0) {
            
            if ([RTJUserTool isLogin]) {
                
                if ([RTJUserTool getTheUserModel].user_autobid == 1) {
                    
                    RTJAutomaticBidDetailsController *VC = [[RTJAutomaticBidDetailsController alloc]init];
                    [self.navigationController pushViewController:VC animated:YES];
                    
                }else{
                    
                    RTJAutomaticBidController *VC = [[RTJAutomaticBidController alloc]init];
                    [self.navigationController pushViewController:VC animated:YES];
                }
                
            }else{
                
                RTJLoginOrRegisterController *vc = [[RTJLoginOrRegisterController alloc]init];
                LXNavigationViewController *nav=[[LXNavigationViewController alloc]initWithRootViewController:vc];
                [self presentViewController:nav animated:YES completion:nil];

            }
            
        }else{
            
            RTJTenderDetailsController *VC = [[RTJTenderDetailsController alloc]init];
            VC.markId = [NSString stringWithFormat:@"%@",self.dict[@"new_deal"][@"id"]];
            VC.isNewBin = YES;
            [self.navigationController pushViewController:VC animated:YES];
            
        }
        
    }else if (indexPath.section == 2) {
        
        RTJTenderDetailsController *VC = [[RTJTenderDetailsController alloc]init];
        VC.markId = [NSString stringWithFormat:@"%@",self.data[indexPath.row-1][@"id"]];
        [self.navigationController pushViewController:VC animated:YES];
        
    } else if (indexPath.section ==1) {
        
        if (indexPath.row!=0) {
            
                RTJAutoMarkDetailsController *vc = [[RTJAutoMarkDetailsController alloc]init];
                vc.markId = [NSString stringWithFormat:@"%@",self.dict[@"plan_list"][indexPath.row-1][@"id"]];
                [self.navigationController pushViewController:vc animated:YES];
        
        }
        
    }else{
        
        RTJTenderDetailsController *VC = [[RTJTenderDetailsController alloc]init];
        VC.markId = [NSString stringWithFormat:@"%@",self.dict[@"new_deal"][@"id"]];
        VC.isNewBin = YES;
        [self.navigationController pushViewController:VC animated:YES];
        
    }
    
}

@end
